package keter.web.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import keter.domain.Account;
import keter.web.routes.RestfulRoutes;
import keter.web.routes.Router;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Maps;

/**
 * Base controller for every controller in application. Provides some basic
 * helper methods useful in controller logic.
 * 
 * @author Dawid Fatyga
 * 
 */
@Transactional
public abstract class KeterBaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(KeterBaseController.class);

	public static class Routes implements RestfulRoutes {
		
	}
	
    public Map<String, String> getAjaxMap(){
      	 /* 前台消息map */
          Map<String, String> ajaxMap = Maps.newHashMap(); 
      	  //构造默认返回消息
          ajaxMap.put("message", "SUCCESS");
   		return ajaxMap;
    }

	/**
	 * Calculates context path for redirection.
	 * 
	 * @param contextPath
	 *            target url
	 * @param arguments
	 *            arguments which will be passed to route
	 * @return context path suitable for redirection
	 */
	public String redirectTo(String action, Object... arguments) {
		return getRoutes().getRoute(action).appendAll(arguments)
				.withRedirection();
	}

	/**
	 * Simple redirection
	 * 
	 * @param action
	 *            target of the recirection
	 */
	public static String redirectToUrl(String action) {
		return "redirect:" + action;
	}
	

	private static Router routes;

	synchronized protected static Router getRouter() {
		if (routes == null) {
			routes = new Router();
		}
		return routes;
	}

	@ModelAttribute("routes")
	public Router getRoutes() {
		return getRouter().populateWith(this);
	}

	public Account getCurrentAccount() {
		if (isLoggedIn()) {
			return SecurityController.getCurrentAccount().getAccount();
		} else {
		    return null;
		}
	}

	public boolean isLoggedIn() {
		return SecurityController.isLoggedIn();
	}
	
	/**
	 * <p>Method ：converPrefix
	 * <p>Description : 将"/path/to" 转换成 "/path/to_"形式，便于区分每个模块的jsp文件名
	 * @param prefix
	 * @return 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	public static String to_(String prefix){
		
		if(!prefix.contains("/")){//"regist"等直接量
			return prefix.concat("/").concat(prefix).concat("_");
		}
		return prefix.concat(prefix.substring(prefix.lastIndexOf("/"),prefix.length())).concat("_");
	}
	
	public static void main(String[] args) {
		System.out.println(to_("regist"));
		System.out.println(to_("paper/regist"));
	}
	
	/**
	 * <p>Method ：populateRootRequestMapping
	 * <p>Description : 将Controller的根映射放入JSTL变量中
	 * @param controllerClass
	 * @return 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	@ModelAttribute("RootRequestMapping")
	public String populateRootRequestMappingOfController() {
		Class<? extends Object> controllerClass = this.getClass();
		if(controllerClass == null || controllerClass.getAnnotation(RequestMapping.class)==null){
    		logger.info("当前class对象为空或未设置RootRequestMapping！");
    		return "";
    	}
		RequestMapping classRoute = controllerClass.getAnnotation(RequestMapping.class);
		logger.info("##############[{}]############",classRoute.value()[0]);
	    return classRoute.value()[0];
	} 
	
}
